<script setup>
import { ref, onMounted } from 'vue'
import { getHotPropertyService } from '@/apis/hot'

// 存储热门房源列表信息
const hotPropertyList = ref([])

// 实现分页需要的参数
const pageSize = ref(4)
const page = ref(1)
const total = ref(null)

// 获取热门房源需要的参数
const hotPropertyItem = ref({
  pageSize: pageSize.value,
  page: page.value
})

// 获取热门房源信息
const getHotProperty = async () => {
  const res = await getHotPropertyService(hotPropertyItem.value)
  hotPropertyList.value = res.data.data.rows
  total.value = res.data.data.count
}

onMounted(() => {
  getHotProperty()
})

// 处理页码变化
const handleCurrentChange = (newPage) => {
  hotPropertyItem.value.page = newPage
  getHotProperty()
}

// 处理页面显示数据条数变化
const handleSizeChange = (newSize) => {
  hotPropertyItem.value.pageSize = newSize
  getHotProperty()
}
</script>

<template>
  <div class="container">
    <div class="header"></div>
    <div class="content" v-if="hotPropertyList.length">
      <ul>
        <li v-for="item in hotPropertyList" :key="item.id">
          <div class="rate">
            <el-rate
              v-if="item.evaluate.length > 0"
              v-model="item.evaluate[0].rating"
              disabled
            />
            <p v-if="item.evaluate.length > 0">
              {{ item.evaluate[0].comment }}
            </p>
            <el-button
              @click="$router.push(`/detail/${item.id}`)"
              type="primary"
              size="large"
              >查看详情</el-button
            >
          </div>
          <div class="property">
            <div class="image">
              <img :src="item.image_url.interior_img[0].url" alt="" />
            </div>
            <h3>{{ item.title }}</h3>
            <p>{{ item.resident }}-{{ item.address }}</p>
            <p>{{ item.room_count }}室{{ item.livingroom_count }}厅</p>
            <p>{{ item.rent_type }}</p>
            <h2>{{ Math.floor(item.price) }} 元/月</h2>
          </div>
        </li>
      </ul>

      <el-row>
        <el-col :offset="8">
          <!-- 分页区域 -->
          <div class="pagination">
            <el-pagination
              background
              layout="jumper,total,sizes,prev,pager,next"
              :page-sizes="[2, 3, 5]"
              v-model:current-page="page"
              v-model:page-size="pageSize"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :total="total"
            ></el-pagination>
          </div>
        </el-col>
      </el-row>
    </div>

    <el-empty v-else :image-size="200" />
  </div>
</template>

<style lang="scss" scoped>
.container {
  width: 80%;
  background-color: #fff;

  .header {
    width: auto;
    height: 650px;
    background: url('@/assets/images/hot.webp') no-repeat center center;
    background-size: cover;
  }

  .content {
    display: flex;
    flex-direction: column;
    align-items: center;

    ul {
      display: flex;
      flex-direction: column;
      align-items: center;

      li {
        width: 1000px;
        height: 450px;
        display: flex;
        margin: 30px;
        border: 2px solid #f5f5f5;
        border-radius: 20px;
        transition: box-shadow 0.5s ease;

        .rate {
          width: 500px;
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;

          ::v-deep .el-icon {
            font-size: 35px;
          }

          p {
            width: 400px;
            height: auto;
            font-size: 20px;
            margin: 30px 0;
            text-align: center;
          }

          button {
            width: 100px;
          }
        }

        .property {
          width: 600px;
          height: 100%;
          display: flex;
          flex-direction: column;
          margin-top: 20px;
          align-items: center;
          text-align: center;
          font-size: 16px;

          .image {
            width: 350px;
            height: 180px;
            margin-bottom: 10px;

            img {
              width: 100%;
              height: 100%;
              border-radius: 20px;
              margin-bottom: 10px;
            }
          }

          p {
            margin: 5px 0;
          }

          h2 {
            display: inline-block;
            font-weight: bold;
            font-size: 25px;
            color: red;
          }
        }

        &:hover {
          box-shadow: 0 0 10px 3px rgba(0, 0, 0, 0.3);
        }
      }
    }

    .pagination {
      margin: 40px;
    }
  }
}
</style>
